package com.rabbiter.oes.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.entity.FillQuestion;
import org.apache.ibatis.annotations.*;

import java.util.List;

//填空题
@Mapper
public interface FillQuestionMapper extends BaseMapper<FillQuestion> {

    @Select("select * from fill_question where questionId in (select questionId from paper_manage where questionType = 2 and paperId = #{paperId})")
    List<FillQuestion> findByIdAndType(String paperId);

    @Select("select * from fill_question")
    IPage<FillQuestion> findAll(Page page);

    /**
     * 查询最后一条questionId
     * @return FillQuestion
     */
    @Select("select questionId from fill_question order by questionId desc limit 1")
    FillQuestion findOnlyQuestionId();

    @Options(useGeneratedKeys = true,keyProperty ="questionId" )
    @Insert("insert into fill_question(subject,question,rightAnswer,optionA,optionB,optionC,optionD,score) values " +
            "(#{subject},#{question},#{rightAnswer},#{optionA},#{optionB},#{optionC},#{optionD},#{score})")
    int add(FillQuestion fillQuestion);

    @Select("select questionId from fill_question where subject = #{subject} order by rand() desc limit #{pageNo}")
    List<Integer> findBySubject(@Param("subject") String subject, @Param("pageNo") Integer pageNo);

    @Update("update fill_question set question = #{question}, subject = #{subject}, rightAnswer = #{rightAnswer}, optionA=#{optionA},optionB = #{optionB},optionC=#{optionC},optionD=#{optionD} where questionId = #{questionId}")
    int edit(FillQuestion fillQuestion);


    @Select("SELECT * FROM fill_question " +
            "WHERE subject = #{subject} " +
            "ORDER BY RAND() LIMIT #{count}")
    List<FillQuestion> findRandom(
            @Param("count") Integer count,
            @Param("subject") String subject
    );

    @Select("select * from fill_question where subject = #{subject}")
    List<FillQuestion> findSequence(String subject);
    @Select(" SELECT fq.* FROM fill_question fq INNER JOIN paper_manage pm ON fq.questionId = pm.questionId  WHERE pm.PaperId = #{PaperId} AND pm.userId = #{userId} AND pm.questionType = 2 ")
    List<FillQuestion> findByExamCodeAndUserId(String PaperId, Long userId);
}
